<template>
  <div>
    <el-dialog
      title="评价"
      :close-on-click-modal="false"
      append-to-body
      :visible.sync="visible"
      class="jsbos-dialog jsbos-dialog_center"
      lock-scroll
      width="600px"
    >
      <el-form
        :model="dataForm"
        :rules="rules"
        ref="dataForm"
        label-width="100px"
      >
        <el-form-item label="评分：" prop="evaluateGrade">
          <el-rate
            v-model="dataForm.evaluateGrade"
            @change="actorChange"
          ></el-rate>
        </el-form-item>
        <el-form-item label="动态描述：" prop="evaluateDescription">
          <el-input
            type="textarea"
            :rows="3"
            v-model="dataForm.evaluateDescription"
            placeholder="请输入"
            clearable
            :style="{ width: '100%' }"
            maxlength="25"
            show-word-limit
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="(visible = false), (submitLoad = true)"
          >取 消</el-button
        >
        <el-button
          type="primary"
          @click="moveSalesSubmit()"
          :loading="submitLoad"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { evaluateOrder } from "@/views/questionnaire/api/complaint";
export default {
  components: {},
  props: [],
  data() {
    return {
      submitLoad: false,
      visible: false,
      enabledMark: null,
      dataForm: {
        id: "",
        evaluateGrade: null,
        evaluateDescription: "",
      },
      rules: {
        evaluateGrade: [
          {
            required: true,
            message: "评分不能为空",
            trigger: "change",
          },
        ],
        evaluateDescription: [
          {
            required: true,
            message: "动态描述不能为空",
            trigger: "change",
          },
        ],
      },
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    actorChange(ids, item) {
      this.dataForm.evaluateDescription = "";
      var userName = this.$store.getters.userInfo.userName;
      var input = userName + "已评价该任务。";
      this.dataForm.evaluateDescription.includes(input)
        ? (this.dataForm.evaluateDescription =
            this.dataForm.evaluateDescription.replace(input, ""))
        : (this.dataForm.evaluateDescription += input);
    },
    moveSalesSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.submitLoad = true;
          if (
            this.dataForm.evaluateDescription[
              this.dataForm.evaluateDescription.length - 1
            ] == "，"
          ) {
            this.dataForm.evaluateDescription =
              this.dataForm.evaluateDescription.substr(
                0,
                this.dataForm.evaluateDescription.length - 1
              );
          }
          this.dataForm.enabledMark = this.enabledMark;

          if (this.enabledMark == "EVALUATED") {
            evaluateOrder(this.dataForm)
              .then((res) => {
                this.visible = false;
                this.$message.success("评价成功！");
                this.submitLoad = false;
                this.$emit("close", true);
              })
              .catch((res) => {
                this.submitLoad = false;
              });
          }
        }
      });
    },
    init(id, enabledMark) {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields();
        this.dataForm.id = id || 0;
        this.enabledMark = enabledMark;
      });
    },
  },
};
</script>
